#include("./config-plugin.html")
#include("./config-jianshu.html")
#include("./config-toutiao.html")
#include("./config-csdn.html")

#@layout()

#define css()
<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
<style>
    img .platformLogo{
        height:1rem;
    }
    .content{
        padding: 20px;
    }
    .switchery-small {
         margin-top: 0px;
     }
    body{
        line-height: 1;
    }
</style>
#end

#define script()
<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script>
<script src="https://cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<script>
    var WEEK_SEC = 3600 * 24 * 7;

    function doverify(platform, conf){
        ajaxPost('#(CAPTH)/admin/articlesync/doVerify', {
            "platform" : platform,
            "conf" : conf
        },  function (result) {
            showMessage(result.message);
        },  function (result) {
            showErrorMessage(result.message);
        })
    }

    function doSave(platform, conf){
        var _doSave = function(){
            ajaxPost('#(CAPTH)/admin/articlesync/doConfig', {
                "platform" : platform,
                "conf" : conf
            },  function (result) {
                showMessage(result.message);
            },  function (result) {
                showErrorMessage(result.message);
            })
        }

        if(conf.enable && conf.cookie && !getCookie("cookieAlarmRead")){
            showCookieAlarm(_doSave);
        } else {
            _doSave();
        }

    }

    function showCookieAlarm(func){
        $.confirm({
            title: "<h3 style='color: red;'>安全警告</h3>",
            text:"<div style='text-align:left;color:#000'>" +
                "1、cookie泄露有安全风险<br/>" +
                "2、本插件不会将cookie用于同步文章以外的传输<br/>" +
                "3、设置的cookie加密存储在jpress设置的数据库option表中<br/>" +
                "<b>4、继续使用前请您确认了解以上问题</b></div>",
            onOK: function () {
                //点击确认
                setCookie("cookieAlarmRead", 1, WEEK_SEC);
                func()
            },
            onCancel: function () {
                $.alert("请尽快卸载插件并检查option表中是否有遗留数据", "");
            }
        })
    }

    function checkEnable(self){
        var checkBox = $(self)
        var boxHeader = checkBox.parent().parent()

        if(checkBox.is(":checked")){
            boxHeader.parent().children().show();
        } else {
            boxHeader.parent().children().not(boxHeader).hide();
        }
    }

    $(".platform-enable").change(function(){
        var checkBox = $(this)
        var boxHeader = checkBox.parent().parent()
        checkEnable(this)
        if(checkBox.is(":checked") != checkBox.prev().val()){
            setTimeout(boxHeader.parent().find(".saveBtn").click(), 200);
        }
    }).each(function(){
        checkEnable(this)
    })

    /**
     * [removeCookie 移除cookie]
     */
    function removeCookie(key){
        setCookie(key,"",-1); // 把cookie设置为过期
    };

    /**
     * [setCookie 设置cookie]
     * [key value t 键 值 时间(秒)]
     */
    function setCookie(key,value,t){
        var oDate=new Date();
        oDate.setDate(oDate.getDate()+t);
        document.cookie=key+"="+value+"; expires="+oDate.toDateString();
    }
    /**
     * [getCookie 获取cookie]
     */
    function getCookie(key){
        var arr1=document.cookie.split("; ");//由于cookie是通过一个分号+空格的形式串联起来的，所以这里需要先按分号空格截断,变成[name=Jack,pwd=123456,age=22]数组类型；
        for(var i=0;i<arr1.length;i++){
            var arr2=arr1[i].split("=");//通过=截断，把name=Jack截断成[name,Jack]数组；
            if(arr2[0]==key){
                return decodeURI(arr2[1]);
            }
        }
    }
</script>
#end

#define content()
<div class="content-wrapper">
    <section class="content-header">
        <h1 ondblclick="removeCookie('cookieAlarmRead')">
            账号设置
            <small>Account Config</small>
        </h1>
    </section>

    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <a class="btn btn-default" href="/admin/articlesync/listPage">
                    <i class="fa fa-fw fa-chevron-left"></i>返回
                </a>
                <div class="btn-group">
                    <button type="button" class="btn btn-default">使用谷歌插件配置</button>
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                        <span class="caret"></span>
                        <span class="sr-only">Toggle Dropdown</span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a target="_blank" href="https://gitee.com/nianzecong/jpress-addon-articlesync/raw/master/src/main/webapp/chrome-ext.crx">下载插件包（.crx）</a></li>
                        <li><a target="_blank" href="https://gitee.com/nianzecong/jpress-addon-articlesync/raw/master/src/main/webapp/chrome-ext.zip">下载源码包（.zip）</a></li>
                        <li class="divider"></li>
                        <li><a target="_blank" href="https://gitee.com/nianzecong/jpress-addon-articlesync/blob/master/doc/chromeExt.md" style="color:red"><i class="fa fa-question-circle"> 如何安装使用</i></a></li>
                    </ul>
                </div>
            </div>
        </div>
        <p></p>

        <!--插件配置-->
        #@content_plugin()

        <!--简书-->
        #@content_jianshu()

        <!--头条-->
        #@content_toutiao()

        <!--CSDN博客-->
        #@content_csdn()

    </section>
</div>
#end
